//
//  SHD_PhotoTebleViewCell.m
//  B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_PhotoTebleViewCell.h"

@implementation SHD_PhotoTebleViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setContentMode:UIViewContentModeScaleToFill];
        self.clipsToBounds= YES;
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textColor = [UIColor grayColor];
        _titleLabel = [[myTitleLabel alloc]init];
        _contectLabel  = [[myContectLabel alloc]init];
        _likeLabel = [[UILabel alloc]init];
        _likeLabel.textColor = [UIColor grayColor];
        _upImageView  = [[UIImageView alloc]init];
        _leftImageView = [[UIImageView alloc]init];
        _rightImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_contectLabel];
        [self.contentView addSubview:_likeLabel];
        [self.contentView addSubview:_upImageView];
        [self.contentView addSubview:_leftImageView];
        [self.contentView addSubview:_rightImageView];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _nameLabel.frame = CGRectMake(15 * FitWidth, 15 * FitHeight, 100 * FitWidth, 10 * FitHeight);
    _upImageView.frame = CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.size.height + _nameLabel.frame.origin.y + 10 * FitHeight, 345 * FitWidth, 170 * FitHeight);
    _leftImageView.frame = CGRectMake(_upImageView.frame.origin.x , _upImageView.frame.size.height + _upImageView.frame.origin.y + 5 * FitHeight, 170 * FitWidth, _upImageView.frame.size.height);
    _rightImageView.frame = CGRectMake(_leftImageView.frame.origin.x + _leftImageView.frame.size.width + 5 *FitWidth, _leftImageView.frame.origin.y, _leftImageView.frame.size.width, _leftImageView.frame.size.height);
    _titleLabel.frame = CGRectMake(_leftImageView.frame.origin.x, _leftImageView.frame.size.height + _leftImageView.frame.origin.y + 10 *FitHeight, 345 *FitWidth, 25 * FitHeight);
    _contectLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.size.height + _titleLabel.frame.origin.y + 10 * FitHeight, _titleLabel.frame.size.width, 60 * FitHeight);
    _likeLabel.frame = CGRectMake(_contectLabel.frame.origin.x, _contectLabel.frame.origin.y + _contectLabel.frame.size.height + 10 * FitHeight, _contectLabel.frame.size.width, 10 * FitHeight);
    _likeLabel.textAlignment = 2;
    _contectLabel.numberOfLines = 0;
    _likeLabel.font = [UIFont systemFontOfSize:10 * FitWidth];
    _nameLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];
    
    _likeLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];

    _upImageView.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(savePhoto:)];
    longPress.minimumPressDuration = 1;
    [_upImageView addGestureRecognizer:longPress];
    _leftImageView.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress2 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(savePhoto:)];
    longPress.minimumPressDuration = 1;
    [_leftImageView addGestureRecognizer:longPress2];

    _rightImageView.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress3 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(savePhoto:)];
    longPress.minimumPressDuration = 1;
    [_rightImageView addGestureRecognizer:longPress3];

    _upImageView.tag = 1101;
    _leftImageView.tag = 1102;
    _rightImageView.tag = 1103;
    
}
#pragma mark 图片保存

-(void)savePhoto:(UILongPressGestureRecognizer *)gesture
{
    UIImageView *imageView = (UIImageView *)gesture.view;
    switch (imageView.tag) {
        case 1101:
            _temp = 1;
            break;
            case 1102:
            _temp = 2;
            break;
            case 1103 :
            _temp = 3;
            break;
            
        default:
            break;
    }

    if (gesture.state == UIGestureRecognizerStateBegan) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:@"提示"
                                      delegate:self
                                      cancelButtonTitle:@"取消"
                                      destructiveButtonTitle:@"保存到相册"
                                      otherButtonTitles: nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [actionSheet showInView:self.contentView];
        
    }
    
    
}
#pragma mark 保存相片方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
       if (buttonIndex == 0) {
           switch (_temp) {
               case 1:
                   UIImageWriteToSavedPhotosAlbum(_upImageView.image, self, NULL, NULL);

                   break;
                   case 2:
                   UIImageWriteToSavedPhotosAlbum(_leftImageView.image, self, NULL, NULL);

                   break;
               case 3:        UIImageWriteToSavedPhotosAlbum(_rightImageView.image, self, NULL, NULL);
                   break;
               default:
                   break;
           }
        
    }
}

-(void)setPhotoList:(SHD_PhotoList *)photoList
{
    _photoList = photoList;
    _nameLabel.text = [NSString stringWithFormat:@"%@ · %@", photoList.Name, photoList.enname];
       [_upImageView sd_setImageWithURL:[NSURL URLWithString:[photoList.Imglist[0] objectForKey:@"imgurl"] ] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]];
   [_leftImageView sd_setImageWithURL:[NSURL URLWithString:[photoList.Imglist[1] objectForKey:@"imgurl"] ] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]];
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:[photoList.Imglist[2] objectForKey:@"imgurl"] ] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]];
    _titleLabel.text = photoList.title;
    _contectLabel.text = photoList.content;
    _contectLabel.textType = [NSNumber numberWithInteger:LabelColorGray];
    _likeLabel.text = [NSString stringWithFormat:@"有%@人喜欢", photoList.like];


    
    
    _upImageView.userInteractionEnabled = YES;

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
